package com.lu.widgets.component.ime;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.lu.widgets.R;
import com.lu.widgets.component.animation.iTranslateAnimation;

public class QwertyNumberIME extends RelativeLayout implements IIMEAnimation {
	private static final String TAG = "QwertyNumberIME";
	private iTranslateAnimation imeGone;
	private iTranslateAnimation imeCurrent;
	private iTranslateAnimation imeNext;
	private iTranslateAnimation imeIn;
	private iTranslateAnimation imeOut;
	private QwertyIMEWithoutMask qwerty;
	private NumberIMEWithoutMask number;
	private View mask;

	public QwertyNumberIME(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		LayoutInflater.from(context).inflate(R.layout.qwerty_number_ime, this,
				true);
		if (!isInEditMode()) {
			findAllView();
			setListener();
			setDefaultProperties();
			TypedArray a = context.obtainStyledAttributes(attrs,
					R.styleable.IME, defStyle, 0);
			android.view.ViewGroup.LayoutParams lp = qwerty.getLayoutParams();
			lp.height = a.getDimensionPixelSize(R.styleable.IME_qwerty_height,
					228);
			qwerty.setLayoutParams(lp);
			lp = number.getLayoutParams();
			lp.height = a.getDimensionPixelSize(R.styleable.IME_number_height,
					160);
			number.setLayoutParams(lp);
			mask.setVisibility(a.getBoolean(R.styleable.IME_enableMask, true) ? VISIBLE
					: GONE);
			a.recycle();
		}
	}

	public QwertyNumberIME(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public QwertyNumberIME(Context context) {
		this(context, null, 0);
	}

	public void setTag(EditText editText) {
		qwerty.setTag(editText);
		number.setTag(editText);
		editText.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				v.performClick();
				v.requestFocus();
				display();
				return true;
			}
		});
	}

	public void findAllView() {
		mask = (View) findViewById(R.id.mask);
		qwerty = (QwertyIMEWithoutMask) findViewById(R.id.qwerty);
		number = (NumberIMEWithoutMask) findViewById(R.id.number);
		imeGone = new iTranslateAnimation(getContext(), number, 0, 300, this,
				false);
		imeGone.setEnableParent(true);
		imeNext = new iTranslateAnimation(getContext(), qwerty, 0, 300, this,
				false);
		imeNext.setEnableParent(true);
		imeCurrent = null;
	}

	protected void setDefaultProperties() {
		setVisibility(GONE);
		qwerty.enableChangeBtn(true);
		number.enableChangeBtn(true);
	}

	protected void setListener() {
		setOnMaskClickListener(null);
		OnClickListener cl = new OnClickListener() {
			@Override
			public void onClick(View v) {
				change();
			}
		};
		qwerty.setOnClickListener(EnumIMEButton.Change, cl);
		number.setOnClickListener(EnumIMEButton.Change, cl);
	}

	public void setOnClickListener(EnumIMEButton btn, final OnClickListener l) {
		switch (btn) {
		case Change:
			OnClickListener cl = new OnClickListener() {
				@Override
				public void onClick(View v) {
					change();
					l.onClick(v);
				}
			};
			qwerty.setOnClickListener(EnumIMEButton.Change, cl);
			number.setOnClickListener(EnumIMEButton.Change, cl);
			break;
		case Delete:
			qwerty.setOnClickListener(EnumIMEButton.Delete, l);
			number.setOnClickListener(EnumIMEButton.Delete, l);
			break;
		case Chars:
			qwerty.setOnClickListener(EnumIMEButton.Chars, l);
			number.setOnClickListener(EnumIMEButton.Chars, l);
			break;
		default:
			break;
		}
	}

	public void setOnMaskClickListener(final OnClickListener l) {
		mask.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				hide();
				if (l != null)
					l.onClick(v);
			}
		});
	}

	public boolean performMaskClick() {
		return mask.performClick();
	}

	public boolean display() {
		Log.d(TAG, "in display()." + (imeCurrent == null));
		if (imeCurrent == null) {
			setVisibility(VISIBLE);
			imeIn = imeNext;

			imeCurrent = imeNext;
			imeNext = imeGone;
			imeGone = imeCurrent;
			imeIn.setEnableParent(true);
			imeIn.flyIn();
			return true;
		}
		return false;
	}

	public boolean change() {
		Log.d(TAG, "in change()." + (imeNext != null));
		if (imeCurrent != null) {
			imeIn = imeNext;
			imeOut = imeCurrent;

			imeCurrent = imeNext;
			imeNext = imeGone;
			imeGone = imeCurrent;
			imeIn.setEnableParent(false);
			imeOut.setEnableParent(false);
			imeIn.flyIn();
			imeOut.flyOut();
			return true;
		}
		return false;
	}

	public boolean hide() {
		Log.d(TAG, "in hide()." + (imeCurrent != null));
		if (imeCurrent != null) {
			imeOut = imeCurrent;

			imeGone = imeNext;
			imeNext = imeCurrent;
			imeCurrent = null;
			imeIn.setEnableParent(true);
			imeOut.setEnableParent(true);
			imeOut.flyOut();
			return true;
		}
		return false;
	}
}